package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import controller.SearchListener;
import controller.SearchMouseListener;
import model.GlobalParams;
import model.SearchTableModel;
import model.TableRenderer;

public class SerachFile extends JFrame {
	
	private static final long serialVersionUID = 1L;
	
	
	public SerachFile(MainFrame frame) {
		this.frame = frame;
		createGUI();
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void createGUI() {
		
		this.setLayout(new BorderLayout());
		this.setMinimumSize(new Dimension(400,400));
		this.setLocationRelativeTo(frame);		
		
		txtSource =  new JTextField(120);
		search = new JButton(GlobalParams.getInstance().stringCollection.getCommandsMenu_Search());
		
		File[] voulme = File.listRoots();  
        partition = new JComboBox(voulme);
		
		JPanel p = new JPanel();
		p.setLayout(new GridBagLayout());
		GridBagConstraints cc = new GridBagConstraints();	
		
		cc.fill = GridBagConstraints.HORIZONTAL ;  
		cc.gridx = 0;
		cc.gridy = 0;
		cc.ipadx = 10;       
        cc.weightx = 0.2;  
        p.add(partition,cc); 
		
        cc.fill = GridBagConstraints.HORIZONTAL ;  
		cc.gridx = 1;
		cc.gridy = 0;
		cc.ipadx = 10;       
        cc.weightx = 0.8;  
        p.add(txtSource,cc);      

        table = new JTable();
        table.setDefaultRenderer(JLabel.class, new TableRenderer());
        
        modelTable = new SearchTableModel();		
		modelTable.UpdateNames();
		modelTable.Clear();		
		table.setModel(modelTable);
		
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setShowGrid(false);	
		table.setShowHorizontalLines(false);
		table.setShowVerticalLines(false);	
		table.getTableHeader().setReorderingAllowed(false);
		table.setFillsViewportHeight(true);	
		
		
		
		table.addMouseListener(new SearchMouseListener(frame));
		table.addKeyListener(new SearchListener(frame));
		
		add(BorderLayout.NORTH , p);
        add(BorderLayout.CENTER, new  JScrollPane(table));
        add(BorderLayout.SOUTH , search);  
		
        search.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				final String pattern = txtSource.getText();
				final File currentPartition = (File) partition.getSelectedItem();
				
				Runnable task = new Runnable() {
					@Override
					public void run() {
						
						if (currentPartition.exists())
							search(currentPartition,pattern);						
					}					
				};
				
				task.run();
			}}
        );
        
        setLocationRelativeTo(frame);
        setAlwaysOnTop(true);
       
        
	}
	
	public void search(File file , String pattern) {	
			
		if (file.isDirectory()) 
		{
			File files[] = file.listFiles() ;
			try {
				for (File f : files) {
					search(f, pattern);			
				}
			} 
			catch (Exception e) {}					
		}
		else {			
			if (file .getName().toLowerCase().contains((pattern.toLowerCase()))) {	
				//System.out.println("znalazlem "  + file .getName().toLowerCase());
				((SearchTableModel)table.getModel()).Add(file);
				((SearchTableModel)table.getModel()).fireTableDataChanged();
			} 						
		}		
	}  
	
	
	private JTable table;
	private SearchTableModel modelTable;	
	private JTextField txtSource;
	private JButton search;
	@SuppressWarnings("rawtypes")
	private JComboBox partition;
	private MainFrame frame;
	
}
